package cn.spikego.advicelite.client.version.component.impl;

import cn.spikego.advicelite.client.version.VersionFinder;
import cn.spikego.advicelite.client.version.component.VersionFinderComponent;

import java.util.HashMap;
import java.util.Map;

public class LegacyVersionFinderComponent extends VersionFinderComponent {
    
    // Map of version to protocol version
    private static final Map<String, Integer> VERSION_PROTOCOLS = new HashMap<>();
    
    static {
        VERSION_PROTOCOLS.put("1.8", 47);
        VERSION_PROTOCOLS.put("1.8.1", 47);
        VERSION_PROTOCOLS.put("1.8.2", 47);
        VERSION_PROTOCOLS.put("1.8.3", 47);
        VERSION_PROTOCOLS.put("1.8.4", 47);
        VERSION_PROTOCOLS.put("1.8.5", 47);
        VERSION_PROTOCOLS.put("1.8.6", 47);
        VERSION_PROTOCOLS.put("1.8.7", 47);
        VERSION_PROTOCOLS.put("1.8.8", 47);
        VERSION_PROTOCOLS.put("1.8.9", 47);
        VERSION_PROTOCOLS.put("1.9", 107);
        VERSION_PROTOCOLS.put("1.9.1", 108);
        VERSION_PROTOCOLS.put("1.9.2", 109);
        VERSION_PROTOCOLS.put("1.9.4", 110);
        VERSION_PROTOCOLS.put("1.10", 210);
        VERSION_PROTOCOLS.put("1.10.1", 210);
        VERSION_PROTOCOLS.put("1.10.2", 210);
        VERSION_PROTOCOLS.put("1.11", 315);
        VERSION_PROTOCOLS.put("1.11.1", 316);
        VERSION_PROTOCOLS.put("1.11.2", 316);
        VERSION_PROTOCOLS.put("1.12", 335);
        VERSION_PROTOCOLS.put("1.12.1", 338);
        VERSION_PROTOCOLS.put("1.12.2", 340);
    }

    @Override
    public VersionFinder.ClientVersion search() {
        // Try different possible class names for legacy versions
        String[] possibleClasses = {
            "bcd", "net.minecraft.util.SharedConstants", 
            "net.minecraft.SharedConstants", "SharedConstants"
        };
        
        String[] versionFields = {"a", "VERSION_STRING", "version"};
        String[] protocolFields = {"b", "PROTOCOL_VERSION", "protocol"};
        
        for(String className : possibleClasses) {
            try {
                var sharedConstants = Class.forName(className);
                
                for(String versionField : versionFields) {
                    for(String protocolField : protocolFields) {
                        try {
                            var vField = sharedConstants.getDeclaredField(versionField);
                            var pField = sharedConstants.getDeclaredField(protocolField);
                            
                            vField.setAccessible(true);
                            pField.setAccessible(true);
                            
                            var version = (String)vField.get(null);
                            
                            if(version != null && VERSION_PROTOCOLS.containsKey(version)) {
                                var protocol = VERSION_PROTOCOLS.get(version);
                                return new VersionFinder.ClientVersion(version, protocol, false);
                            }
                        } catch(Exception e) {
                            // Continue trying other combinations
                        }
                    }
                }
            } catch(Exception e) {
                // Continue trying other classes
            }
        }
        
        return null;
    }
}